package org.lwxyz.qiniu.upload;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import org.lwxyz.qiniu.auth.Authorize;

import java.io.File;

/**
 * @description 文件上传
 * @author yuwei
 * @version 1.0-SNAPSHOT
 * @date 2018/1/31
 */
public final class UploadFile {
    /**
     * 上传到七牛云
     * @param file
     * @return
     * @throws QiniuException
     */
    public static String upoload(File file) throws QiniuException {
        Configuration cfg = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(cfg);
        Response response = uploadManager.put(file.getPath(), file.getName(), Authorize.getToken());
        //解析上传成功的结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        return putRet.toString();
    }

    public static void main(String[] args) {
        File file = new File("E:\\personal\\WebSocket-API-demos\\Websocket-Python3-demo\\1.txt");
        try {
            upoload(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
